/*
 * 
 * Created on Jul 11, 2005
 */
package com.wgo.precise.client.ui.view.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource2;
import org.eclipse.ui.views.properties.PropertyDescriptor;

import com.wgo.precise.client.ui.model.AbstractPropertySpesification;
import com.wgo.precise.client.ui.model.Configuration;
import com.wgo.precise.client.ui.model.PropertyValue;
import com.wgo.precise.client.ui.model.facade.ILabelProviderItem;
import com.wgo.precise.client.ui.model.facade.IPropertiesContainer;


/**
 * 
 * @author Petter L. H. Eide
 */
public class ModelItemPropertySource implements IPropertySource2 {

    private IPropertiesContainer item;

    private String category = "Basic";
    private List<AbstractPropertySpesification> additionalDefinitions = new ArrayList<AbstractPropertySpesification>();
    
    public ModelItemPropertySource(IPropertiesContainer node) {
        this.item = node;
    }

    /**
     * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object)
     */
    public boolean isPropertyResettable(Object id) {
        boolean resettable = false;
        PropertyValue property = item.getCurrentPropertyValue((AbstractPropertySpesification)id);
        if (property.getPropertySpesification().hasDefaultValue()) {
            resettable = true;
        }
        return resettable;
    }

    /**
     * Should always return true, unless its out of sync or something.
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertySet(java.lang.Object)
     */
    public boolean isPropertySet(Object id) {
        return item.isPropertySet((AbstractPropertySpesification) id);
    }

    /**
     * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
     */
    public Object getEditableValue() {
        return this;
    }

    /**
     * Gets the descriptors from the item-properties.
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
     */
    public IPropertyDescriptor[] getPropertyDescriptors() {
        List<AbstractPropertySpesification> specs = new ArrayList<AbstractPropertySpesification>();
        specs.addAll(Configuration.getInstance().getSortedNodeSpesifications());
        specs.addAll(additionalDefinitions);
        PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[specs.size()];
        propertyDescriptors = createPropertyDescriptors(specs, category);
        return propertyDescriptors;
    }
    
    protected PropertyDescriptor[] createPropertyDescriptors(Collection<AbstractPropertySpesification> specs, String theCategory) {
        PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[specs.size()];
        int i = 0;
        for (AbstractPropertySpesification spec : specs) {
            PropertyValue property = item.getCurrentPropertyValue(spec);
            if (property == null) {
                item.setPropertyValueFromCellEditor(spec,null);
                property = item.getCurrentPropertyValue(spec);
            }
            propertyDescriptors[i] = property.getDescriptor();

            if (null != theCategory) {
            	propertyDescriptors[i].setCategory(theCategory);
            }
            i++;
        }
        return propertyDescriptors;
    }
    /**
     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
     */
    public Object getPropertyValue(Object id) {
        return item.getCurrentPropertyValue((AbstractPropertySpesification)id).getCellEditorValue();
    }

    /**
     * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
     */
    public void resetPropertyValue(Object id) {
        item.setPropertyDefaultValue(item.getCurrentPropertyValue((AbstractPropertySpesification)id).getPropertySpesification());
    }

    /**
     * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
     */
    public void setPropertyValue(Object id, Object value) {
        AbstractPropertySpesification spec = item.getCurrentPropertyValue((AbstractPropertySpesification)id).getPropertySpesification();
        item.setPropertyValueFromCellEditor(spec, value);
    }

    public IPropertiesContainer getItem() {
        return item;
    }

	@Override
	public String toString() {
		String string = super.toString();
		if (null != item) {
			string = ((ILabelProviderItem)item).getSimpleName();
		}
		return string;
	}

	public void addAbstractPropertySpesification(AbstractPropertySpesification definition) {
		additionalDefinitions.add(definition);
	}
    
}
